package com.supervision.student;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.supervision.student.util.SaveNum;

import java.net.URL;
import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class NumActivity extends AppCompatActivity {

    String phone,password,webIp;
    EditText etPhone, etPassword, etIP;
    Button binLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_num);

        etPhone = findViewById(R.id.et_phone);
        etPassword = findViewById(R.id.et_password);
        etIP = findViewById(R.id.et_ip);
        binLogin = findViewById(R.id.btn_login);

        Map<String, String> userInfo = SaveNum.getUserInfo(this);
        if (userInfo != null) {
            etPhone.setText(userInfo.get("phone"));
            etPassword.setText(userInfo.get("password"));
            etIP.setText(userInfo.get("webIp"));
        }

        binLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phone = etPhone.getText().toString();
                password = etPassword.getText().toString();
                webIp = etIP.getText().toString();

                //保存账号信息
                save();

                LoginAT loginAT = new LoginAT();
                loginAT.execute(webIp, phone, password);
            }
        });

    }

    class LoginAT extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... strings) {
            OkHttpClient okHttpClient = new OkHttpClient();//创建单例
            URL url;
            Request request;//创建请求
            String result = null;
            try {
                //设置url
                url = new URL("http://" + strings[0] + ":8080/stuLogin/" + strings[1] + "/" + strings[2]);
                //设置请求
                request = new Request.Builder()
                        .url(url)
                        .build();
                //执行请求
                Response response = okHttpClient.newCall(request).execute();

                result = response.body().string();
            } catch (Exception e) {
            }
            return result;
        }

        @Override
        protected void onPostExecute(String s) {
            if (s != null) {
                StartService();
                jump();
            }
        }

    }

    void StartService(){
        Intent intent = new Intent(this,PositioningService.class);
        intent.putExtra("webIp",webIp);
        intent.putExtra("phone",phone);
        startService(intent);
    }

    void jump() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    void save() {
        SaveNum.saveUserInfo(this, phone, password, webIp);
    }

}
